<template>
    <div class="scada-model">
        <el-card v-show="showSearch" class="search-card">
            <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" class="search-form">
                <el-form-item prop="modelName">
                    <el-input v-model="queryParams.modelName" :placeholder="$t('scada.model.649850-1')" clearable @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item prop="success">
                    <el-select v-model="queryParams.success" :placeholder="$t('scada.model.649850-6')" clearable>
                        <el-option :label="$t('scada.model.649850-3')" :value="0"></el-option>
                        <el-option :label="$t('scada.model.649850-4')" :value="1"></el-option>
                    </el-select>
                </el-form-item>
                <div style="float: right">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{ $t('search') }}</el-button>
                    <el-button icon="el-icon-refresh" @click="handleResetQuery">{{ $t('reset') }}</el-button>
                </div>
            </el-form>
        </el-card>

        <el-card>
            <el-row :gutter="10" style="margin-bottom: 16px">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="small" @click="handleAdd" v-hasPermi="['scada:model:add']">{{ $t('add') }}</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button plain icon="el-icon-edit" size="small" :disabled="single" @click="handleUpdate" v-hasPermi="['scada:model:edit']">{{ $t('update') }}</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button plain icon="el-icon-delete" size="small" :disabled="multiple" @click="handleDelete" v-hasPermi="['scada:model:remove']">{{ $t('del') }}</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button plain icon="el-icon-download" size="small" @click="handleExport" v-hasPermi="['scada:model:export']">{{ $t('export') }}</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-table v-loading="loading" :data="modelList" :border="false" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column :label="$t('scada.model.649850-0')" align="left" prop="modelName" min-width="230" />
                <el-table-column :label="$t('scada.component.302923-5')" align="center" prop="imageUrl" width="100">
                    <template slot-scope="scope">
                        <image-preview :src="scope.row.imageUrl" :width="48" :height="48" />
                    </template>
                </el-table-column>
                <el-table-column :label="$t('scada.model.649850-5')" align="left" prop="modelUrl" min-width="260" />
                <el-table-column :label="$t('scada.model.649850-2')" align="center" prop="status" width="100">
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.status === 0 ? 'success' : 'danger'">{{ scope.row.status === 0 ? '否' : '是' }}</el-tag>
                    </template>
                </el-table-column>

                <el-table-column fixed="right" :label="$t('opation')" align="center" width="180">
                    <template slot-scope="scope">
                        <el-button size="small" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['scada:model:edit']">{{ $t('update') }}</el-button>
                        <el-button size="small" type="text" icon="el-icon-view" @click="handlePreview(scope.row)" v-hasPermi="['scada:model:preview']">{{ $t('preview') }}</el-button>
                        <el-button size="small" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['scada:model:remove']">{{ $t('del') }}</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination style="margin-bottom: 20px" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
        </el-card>

        <!-- 添加或修改模型管理对话框 -->
        <el-dialog :title="dialog.title" :visible.sync="dialog.open" width="600px" append-to-body>
            <el-form ref="dialogForm" :model="dialog.form" :rules="dialog.rules" label-width="100px">
                <el-form-item :label="$t('scada.component.302923-5')" prop="imageUrl">
                    <image-upload v-model="dialog.form.imageUrl" :multiple="false" :class="{ disable: uploadDisabled }" />
                </el-form-item>
                <el-form-item :label="$t('scada.model.649850-0')" prop="modelName">
                    <el-input v-model="dialog.form.modelName" :placeholder="$t('scada.model.649850-1')" clearable style="width: 400px" />
                </el-form-item>
                <el-form-item :label="$t('scada.model.649850-2')" prop="status">
                    <el-select v-model="dialog.form.status" :placeholder="$t('scada.model.649850-6')" clearable style="width: 400px">
                        <el-option :label="$t('scada.model.649850-3')" :value="0"></el-option>
                        <el-option :label="$t('scada.model.649850-4')" :value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item :label="$t('scada.model.649850-5')" prop="modelUrl">
                    <el-input v-model="dialog.form.modelUrl" type="textarea" :autosize="{ minRows: 3, maxRows: 5 }" :placeholder="$t('scada.model.649850-7')" clearable style="width: 400px" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleDialogSubmit">{{ $t('confirm') }}</el-button>
                <el-button @click="handleDialogCancel">{{ $t('cancel') }}</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listModel, getModel, delModel, addModel, updateModel } from '@/api/scada/model';

export default {
    name: 'Model',
    computed: {
        uploadDisabled: function () {
            return this.dialog.form.imageUrl !== '';
        },
    },
    data() {
        return {
            loading: true, // 遮罩层
            ids: [], // 选中数组
            single: true, // 非单个禁用
            multiple: true, // 非多个禁用
            showSearch: true, // 显示搜索条件
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                modelName: null,
                status: null,
            },
            modelList: [], // 模型管理表格数据
            total: 0, // 总条数
            dialog: {
                open: false, // 弹出层标题
                title: '', // 对话框标题
                // 表单参数
                form: {
                    imageUrl: '',
                    modelName: '',
                    status: 0,
                    modelUrl: '',
                },
                // 表单校验
                rules: {
                    modelName: [{ required: true, message: this.$t('scada.model.649850-1'), trigger: 'change' }],
                    status: [{ required: true, message: this.$t('scada.model.649850-6'), trigger: 'change' }],
                    modelUrl: [{ required: true, message: this.$t('scada.model.649850-7'), trigger: 'change' }],
                },
            },
        };
    },
    created() {
        this.getList();
    },
    methods: {
        // 查询模型管理列表
        getList() {
            this.loading = true;
            listModel(this.queryParams).then((res) => {
                if (res.code === 200) {
                    this.modelList = res.rows;
                    this.total = res.total;
                }
                this.loading = false;
            });
        },
        // 搜索按钮操作
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        // 重置按钮操作
        handleResetQuery() {
            this.resetForm('queryForm');
            this.handleQuery();
        },
        // 新增按钮操作
        handleAdd() {
            this.reset();
            this.dialog.open = true;
            this.dialog.title = this.$t('scada.model.649850-8');
        },
        // 表单重置
        reset() {
            this.dialog.form = {
                imageUrl: '',
                modelName: '',
                status: 0,
                modelUrl: '',
            };
            this.resetForm('dialogForm');
        },
        // 取消按钮
        handleDialogCancel() {
            this.dialog.open = false;
        },
        // 提交按钮
        handleDialogSubmit() {
            this.$refs['dialogForm'].validate((valid) => {
                if (valid) {
                    if (this.dialog.form.id != null) {
                        updateModel(this.dialog.form).then((res) => {
                            if (res.code === 200) {
                                this.$modal.msgSuccess(this.$t('updateSuccess'));
                                this.dialog.open = false;
                                this.getList();
                            }
                        });
                    } else {
                        addModel(this.dialog.form).then((res) => {
                            if (res.code === 200) {
                                this.$modal.msgSuccess(this.$t('addSuccess'));
                                this.dialog.open = false;
                                this.getList();
                            }
                        });
                    }
                }
            });
        },
        // 修改按钮操作
        handleUpdate(row) {
            this.dialog.title = this.$t('scada.model.649850-9');
            const id = row.id || this.ids;
            getModel(id).then((res) => {
                if (res.code === 200) {
                    this.dialog.form = res.data;
                    this.dialog.open = true;
                }
            });
        },
        // 预览
        handlePreview(row) {
            if (row.modelUrl) {
                window.open(row.modelUrl);
            } else {
                this.$message.error(this.$t('scada.model.649850-10'));
            }
        },
        // 删除按钮操作
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm(this.$t('scada.model.649850-11', [ids]))
                .then(function () {
                    return delModel(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess(this.$t('delSuccess'));
                })
                .catch(() => {});
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        // 导出按钮操作
        handleExport() {
            this.download(
                'scada/model/export',
                {
                    ...this.queryParams,
                },
                `模型${new Date().getTime()}.xlsx`
            );
        },
    },
};
</script>

<style lang="scss" scoped>
.scada-model {
    padding: 20px;

    .search-card {
        margin-bottom: 15px;
        padding: 3px 0;
    }

    .search-form {
        margin-bottom: -22.5px;
    }
}

.disable {
    ::v-deep .el-upload--picture-card {
        display: none !important;
    }
}
</style>
